package com.example.demo.Service;

import com.example.demo.Repository.UserRepository;
import com.example.demo.userdemo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    // 1. 根据User对象查询
    public User getUser(User user) {
        return userRepository.findById(user.getId()).orElse(null);
    }

    // 2. 根据User对象修改
    @Transactional
    public User updateUser(User user) {
        return userRepository.save(user);
    }

    // 3. 批量删除
    @Transactional
    public void deleteUsers(List<Long> ids) {
        userRepository.deleteAllById(ids);
    }

    // 4. 批量添加
    @Transactional
    public List<User> addUsers(List<User> users) {
        return userRepository.saveAll(users);
    }
}